#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <unordered_map>
#include <vector>
#include <set>
#include <queue>

using namespace std;
using LL = long long;
using PII = pair<int, int>;
const int N = 2e5 + 10;

int n, k;
int p[N];

int find(int x){
    if(x != p[x]){
        p[x] = find(p[x]);
    }
    return p[x];
}

void merge(int a, int b){
    int fa = find(a), fb = find(b);
    p[fa] = fb;
}

int main(){
#ifdef LOCALFLAG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif


    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> k;
    for(int i = 1; i <= n * 3; i ++) p[i] = i;

    int res = 0;
    int op, a, b;
    while(k --){
        cin >> op >> a >> b;

        if(a > n || b > n) {
            res ++;
            continue;
        }

        if(op == 2 && a == b){
            res ++;
            continue;
        }

        if(op == 1){
            if(find(a) == find(b + n) || find(a + n) == find(b)){
                res ++;
            }else{
                merge(a, b);
                merge(a + n, b + n);
                merge(a + 2 * n, b + 2 * n);
            }
        }else{ // a -> b
            if(find(a) == find(b) || find(b + n) == find(a)){
                res ++;
            }else{
                merge(b, a + n);
                merge(b + 2 * n, a);
                merge(b + n, a + 2 * n);
            }
        }
    }

    cout << res << '\n';

    return 0;
} 